package org.example.com.service.impl;

import org.example.com.mapper.ServiceConfigMapper;
import org.example.com.mapper.ServiceReservationMapper;
import org.example.com.pojo.ServiceConfig;
import org.example.com.pojo.ServiceReservation;
import org.example.com.pojo.vo.ServiceReservationVO;
import org.example.com.service.ServiceReservationService;
import org.example.com.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务预约Service实现类
 */
@Service
public class ServiceReservationServiceImpl implements ServiceReservationService {
    
    @Autowired
    private ServiceReservationMapper serviceReservationMapper;
    
    @Autowired
    private ServiceConfigMapper serviceConfigMapper;
    
    @Override
    public int submit(ServiceReservation serviceReservation) {
        // 获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        int userId = (int) claims.get("id");
        serviceReservation.setUserId(userId);
        
        // 设置默认状态
        serviceReservation.setStatus("待确认");
        
        // 设置时间
        serviceReservation.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        serviceReservation.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        
        // 获取服务配置信息
        ServiceConfig serviceConfig = serviceConfigMapper.findById(serviceReservation.getServiceId());
        if (serviceConfig != null) {
            serviceReservation.setServiceName(serviceConfig.getName() != null ? serviceConfig.getName() : serviceConfig.getTitle());
            serviceReservation.setServiceType(serviceConfig.getConfigType());
            
            // 设置价格
            if (serviceConfig.getPriceAfterDiscounts() != null) {
                String priceStr = serviceConfig.getPriceAfterDiscounts().replace("¥", "");
                try {
                    serviceReservation.setTotalPrice(new BigDecimal(priceStr));
                } catch (NumberFormatException e) {
                    serviceReservation.setTotalPrice(new BigDecimal("0"));
                }
            }
        }
        
        // 保存到数据库
        serviceReservationMapper.submit(serviceReservation);
        return serviceReservation.getId();
    }
    
    @Override
    public List<ServiceReservationVO> getHistory() {
        // 获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        int userId = (int) claims.get("id");
        
        // 获取预约列表
        List<ServiceReservation> reservations = serviceReservationMapper.getHistoryByUserId(userId);
        
        // 转换为VO
        List<ServiceReservationVO> voList = new ArrayList<>();
        for (ServiceReservation reservation : reservations) {
            ServiceReservationVO vo = convertToVO(reservation);
            voList.add(vo);
        }
        
        return voList;
    }
    
    @Override
    public List<ServiceReservationVO> getByStatus(String status) {
        // 获取当前用户信息
        Map<String, Object> claims = ThreadLocalUtil.get();
        int userId = (int) claims.get("id");
        
        // 获取指定状态的预约列表
        List<ServiceReservation> reservations = serviceReservationMapper.getByUserIdAndStatus(userId, status);
        
        // 转换为VO
        List<ServiceReservationVO> voList = new ArrayList<>();
        for (ServiceReservation reservation : reservations) {
            ServiceReservationVO vo = convertToVO(reservation);
            voList.add(vo);
        }
        
        return voList;
    }
    
    @Override
    public ServiceReservationVO getDetail(int id) {
        ServiceReservation reservation = serviceReservationMapper.getById(id);
        if (reservation == null) {
            return null;
        }
        return convertToVO(reservation);
    }
    
    @Override
    public void updateStatus(int id, String status) {
        serviceReservationMapper.updateStatus(id, status);
    }
    
    @Override
    public void cancel(int id) {
        serviceReservationMapper.updateStatus(id, "已取消");
    }
    
    @Override
    public void delete(int id) {
        serviceReservationMapper.delete(id);
    }
    
    @Override
    public List<ServiceReservationVO> getAll() {
        List<ServiceReservation> reservations = serviceReservationMapper.getAll();
        
        // 转换为VO
        List<ServiceReservationVO> voList = new ArrayList<>();
        for (ServiceReservation reservation : reservations) {
            ServiceReservationVO vo = convertToVO(reservation);
            voList.add(vo);
        }
        
        return voList;
    }
    
    /**
     * 将ServiceReservation转换为ServiceReservationVO
     */
    private ServiceReservationVO convertToVO(ServiceReservation reservation) {
        ServiceReservationVO vo = new ServiceReservationVO();
        BeanUtils.copyProperties(reservation, vo);
        
        // 获取服务配置信息
        ServiceConfig serviceConfig = serviceConfigMapper.findById(reservation.getServiceId());
        if (serviceConfig != null) {
            vo.setServiceIcon(serviceConfig.getIcon());
            vo.setServiceImg(serviceConfig.getImg());
            vo.setOriginalPrice(serviceConfig.getOriginalPrice());
            vo.setPriceAfterDiscounts(serviceConfig.getPriceAfterDiscounts());
        }
        
        return vo;
    }
}
